package com.ohc.leetcode.数组;

/**
 * 2022年5月17日15:49:54
 * 独立过
 */
public class 搜索插入位置35 {


    public static int searchInsert(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        int mid = 0;
        while (left <= right) {
            mid = (left + right) / 2;
            if (nums[mid] < target) {
                left = mid + 1;
            } else if (nums[mid] > target) {
                right = mid - 1;
            } else if (nums[mid] == target) {
                return mid;
            }
        }
        if (target < nums[mid]) {
            return mid;
        } else {
            return mid + 1;
        }
    }

    public static void main(String[] args) {
//        6 4
        int[] arr = new int[]{1, 3, 5, 6};
        int i = searchInsert(arr, 0);
        System.out.println(i);
    }

}
